#include "frm_exercises_current.h"
#include "ui_frm_exercises_current.h"
#include "frm_routine_menu.h"
#include "frm_menu.h"
#include <QFile>
#include <QTextStream>
#include <QStringList>
#include <QDesktopWidget>
#include "frm_beats.h"

Frm_exercises_current::Frm_exercises_current(const QString& name, const QString& initial, const QString& imginitial, const QString& final, const QString& imgfinal,QStringList& ejercicios ,QDialog *parent) :
    QDialog(parent),
    ui(new Ui::Frm_exercises_current)
{
    ui->setupUi(this);
    ejercicios_=ejercicios;
    ui->exerciseName->setText(name);
    ui->initialdoing_3->setText(initial);
    QPixmap imagen(imginitial);
    ui->initialimg_3->setPixmap(imagen);
    ui->initialimg_3->setScaledContents(true);
    ui->initialimg_3->setFixedSize(200, 200);
    ui->finaldoing_3->setText(final);
    QPixmap imagenf(imgfinal);
    ui->finalimg_3->setPixmap(imagenf);
    ui->finalimg_3->setScaledContents(true);
    ui->finalimg_3->setFixedSize(200, 200);
    setWindowFlags( Qt:: WindowTitleHint | Qt:: CustomizeWindowHint | Qt:: WindowMinimizeButtonHint | Qt:: WindowMaximizeButtonHint);
    this->move(QApplication::desktop()->screen()->rect().center() - this->rect().center());
}

Frm_exercises_current::~Frm_exercises_current()
{
    delete ui;
}

void Frm_exercises_current::on_btn_menu_3_clicked()
{
    frm_routine_menu *free_exer = new frm_routine_menu();
    this->hide();
    free_exer->show();
}

void Frm_exercises_current::on_btn_next_clicked()
{
    if (ejercicios_.isEmpty())
      {
          frm_menu *free_exer = new frm_menu();
          this->hide();
          free_exer->show();
      }
    else
      {
          QString exercise=ejercicios_.front();
          ejercicios_.pop_front();
          QFile nextExercise(exercise + ".txt");
          if(!nextExercise.open(QIODevice::ReadOnly))
              qFatal("can't open this file");
          QTextStream in(&nextExercise);
          QString name, initial,imginitial,final,imgfinal;

          name = exercise;

          QVector<QString> data(4);
          QString dato;
          int i = 0;
          while (!in.atEnd()){
              dato = in.readLine();
              if (dato != "")
                  if (i == 1 || i == 3)
                      data[i]= dato;
                  else
                      data[i] = data[i] + "\n" + dato;
              else
                  i++;
              }

          initial = data[0];
          imginitial = data[1];
          final = data[2];
          imgfinal = data[3];

         /* name=in.readLine();
          initial=in.readLine();
          imginitial=in.readLine();
          final=in.readLine();
          imgfinal=in.readLine();*/

          ui->exerciseName->setText(name);
          ui->initialdoing_3->setText(initial);
          QPixmap imagen(imginitial);
          ui->initialimg_3->setPixmap(imagen);
          ui->initialimg_3->setScaledContents(true);
          ui->finaldoing_3->setText(final);
          QPixmap imagenf(imgfinal);
          ui->finalimg_3->setPixmap(imagenf);
          ui->finalimg_3->setScaledContents(true);

          /*ui->initialimg_3->setText(imginitial);
          ui->finaldoing_3->setText(final);
          ui->finalimg_3->setText(imgfinal);*/
      }
}

void Frm_exercises_current::on_btn_beats_clicked()
{
    frm_beats *beats = new frm_beats(this);
    this->hide();
    beats->show();
}
